<?php 
require_once('../config.php');
require_once('include_dao.php');
require_once('logger.php');

//deleteOrphans();
//deleteAllOrphans(2);

function checktree($firstlink){

//echo "star<br>";
	$node = DAOFactory::getQuestionmasterDAO()->load($firstlink);
//echo "node<br>";
	if(!$node->firstLink == null&& $node->yesQuestionId==-1 &&  $node->noQuestionId =-1 && $node->questionType ==2){
//echo "if<br>";
		return true;
	}else if(!$node->firstLink == null && $node->yesQuestionId != -1 &&  $node->noQuestionId != -1){
//echo "elif<br>";
		return (checktree($node->yesQuestionId) && checktree($node->noQuestionId));
	}else{
		//echo "else<br>";
		return false;
	}

}
function deleteAllOrphans($firstlink){
	$arr = DAOFactory::getQuestionmasterDAO()->queryByFirstLink($firstlink);
	for ($index = 2; $index < count($arr); $index++) {
		deleteOrphans($arr[$index]->questionMasterId);
	}
	
	
}


function deleteOrphans($firstlink){

//echo "star " . $firstlink . " -<br>";
	$node = DAOFactory::getQuestionmasterDAO()->load($firstlink);
//echo "node<br>" . $node->parentQuestionId . "<br>";

	$parentNode = DAOFactory::getQuestionmasterDAO()->load($node->parentQuestionId);

	if($parentNode->yesQuestionId != $node->questionMasterId && $parentNode->noQuestionId != $node->questionMasterId){
		//echo "deleting" . $node->questionMasterId . "<br>";
		DAOFactory::getQuestionmasterDAO()->delete($node->questionMasterId);
	}else{
		//echo "not deleting" . $node->questionMasterId . " parents child nodes" . $parentNode->yesQuestionId . " , " . $parentNode->noQuestionId . "<br>"; 
	}
	
}

?>
